package com.zyk.scaffold.core.domain;

import com.zyk.scaffold.common.basic.BasicErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * 全局返回结果集
 *
 * @param <T>
 */
@Data
@ApiModel("响应信息")
public class Result<T> {
    private static int ARGUMENT_NOT_VALID_EXCEPTION = 9999;
    @ApiModelProperty(value = "响应码", required = true)
    private int code;
    @ApiModelProperty(value = "响应提示", required = true)
    private String message;
    @ApiModelProperty(value = "响应数据", required = true)
    private T data;
    @ApiModelProperty(value = "堆栈信息", required = true)
    private String stack;

    public static <T> Result<T> ok(T body) {
        Result<T> result = new Result<T>();
        result.setCode(HttpStatus.OK.value());
        result.setData(body);
        return result;
    }

    public static <T> Result<T> ok() {
        Result<T> result = new Result<T>();
        result.setCode(HttpStatus.OK.value());
        return result;
    }

    public static <T> Result<T> ok(String message) {
        Result<T> result = new Result<T>();
        result.setCode(HttpStatus.OK.value());
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> ok(String message, T body) {
        Result<T> result = new Result<T>();
        result.setCode(HttpStatus.OK.value());
        result.setMessage(message);
        result.setData(body);
        return result;
    }

    public static <T> Result<T> error(BasicErrorCode basicErrorCode) {
        Result<T> result = new Result<T>();
        result.setCode(basicErrorCode.getCode());
        result.setMessage(basicErrorCode.getMessage());
//        result.setStack(basicErrorCode.getStack());
        return result;
    }
}
